# Maintainer: Peter Budai <peterbud@hotmail.com>

_realname=openlibm
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.8.1
pkgrel=1
pkgdesc="High quality system independent, portable, open source libm implementation (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://openlibm.org'
license=('MIT')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc")
options=('strip' 'staticlibs')
source=("${_realname}-${pkgver}.tar.gz::https://github.com/JuliaMath/${_realname}/archive/v${pkgver}.tar.gz")
sha256sums=('ba8a282ecd92d0033f5656bb20dfc6ea3fb83f90ba69291ac8f7beba42dcffcf')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
}

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  cp -rf "${srcdir}/${_realname}-${pkgver}" "${srcdir}/build-${MSYSTEM}"

  cd "${srcdir}/build-${MSYSTEM}"
  local _compilersetting="USEGCC=1"
  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    _compilersetting="USECLANG=1"
  fi

  make prefix=${MINGW_PREFIX} ${_compilersetting}
}

check() {
  cd "${srcdir}"/build-${MSYSTEM}
  make test
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  make prefix=${MINGW_PREFIX} DESTDIR="${pkgdir}" install

  install -Dm644 ${srcdir}/build-${MSYSTEM}/LICENSE.md ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
